package com.example.user.customviewdemo.test;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.example.user.customviewdemo.R;

/**
 * Created by user on 2016/5/10.
 */
public class CircleImageView extends View {
    private Paint paint;
    private Bitmap srcBmp,dstBmp;
    private int src;
    private float borderWidth;
    private int borderColor;

    public CircleImageView(Context context) {
        this(context,null);
    }

    public CircleImageView(Context context, AttributeSet attrs) {
        this(context,attrs,0);
    }

    public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView,defStyleAttr,0);
        int size = a.getIndexCount();
        for (int i = 0; i < size; i++) {
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.CircleImageView_src:
                    src = a.getResourceId(attr,R.mipmap.ic_launcher);
                    srcBmp = BitmapFactory.decodeResource(getResources(),src);
                    break;
                case R.styleable.CircleImageView_borderColor:
                    borderColor = a.getColor(attr,0xffffffff);
                    Log.v("tag","borderColor:"+borderColor);
                    break;
                case R.styleable.CircleImageView_borderWidth:
                    borderWidth = a.getDimensionPixelSize(attr,10);
                    Log.v("tag","borderWidth:"+borderWidth);
                    break;
            }

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        paint.setColor(Color.RED);
        dstBmp = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_4444);
        Canvas c = new Canvas(dstBmp);
        if(getWidth() > getHeight()) {
            c.drawCircle(getWidth() / 2 , getHeight() / 2 ,getHeight()/2 , paint);
        }else {
            c.drawCircle(getWidth()/2 , getHeight()/2 ,getWidth()/2,paint);
        }

        int layerID = canvas.saveLayer(0,0,getWidth(),getHeight(),null,Canvas.ALL_SAVE_FLAG);
        canvas.drawBitmap(dstBmp,getWidth()/2 - dstBmp.getWidth()/2,getHeight()/2 - dstBmp.getHeight()/2,paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(srcBmp,getWidth()/2 - srcBmp.getWidth()/2,getHeight()/2 - srcBmp.getHeight()/2,paint);
        paint.setXfermode(null);
        canvas.restoreToCount(layerID);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if(widthMode == MeasureSpec.EXACTLY){
            width = widthSize;
        }else {
            width = srcBmp.getWidth() + getPaddingLeft() + getPaddingRight();
        }

        if (heightMode == MeasureSpec.EXACTLY){
            height = heightSize;
        }else {
            height = srcBmp.getHeight() + getPaddingBottom() + getPaddingTop();
        }

        setMeasuredDimension(width,height);
    }

}
